package cn.ecut.controller;


import cn.ecut.model.Guanliyuan;
import cn.ecut.model.Message;
import cn.ecut.model.Student;
import cn.ecut.service.GuanliyuanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/guanliyuan")
public class GuanliyuanController {
    @Autowired
    private GuanliyuanService guanliyuanService;

    @ResponseBody
    @RequestMapping("/login")
    public Message checkLogin(HttpSession session , String name, String password){
        Message message = new Message();
        String p = guanliyuanService.selectPasswordByname(name);
        if(password.equals(p)){
            session.setAttribute("name",name);
            session.setAttribute("password",password);
            message.setFlag(true);
        }else {
            message.setFlag(false);
            message.setMsg("用户名或密码错误");
        }
        return message;
    }

    @ResponseBody
    @RequestMapping("/changepassword")
    public Message changepassword(HttpSession session,String password1,String password2){
        Message message =new Message();
        String name = (String) session.getAttribute("name");
        if(password1.equals(guanliyuanService.selectPasswordByname(name))){
            Guanliyuan guanliyuan = new Guanliyuan();
            guanliyuan.setName(name);
            guanliyuan.setPassword(password2);
            int role = guanliyuanService.updatePasswordByname(guanliyuan);
            if(role == 1){
                message.setFlag(true);
                return message;
            }else {
                message.setFlag(false);
                message.setMsg("修改密码失败");
                return message;
            }
        }
        message.setFlag(false);
        message.setMsg("原密码有误");
        return message;
    }

    @ResponseBody
    @RequestMapping("/addGuanliyuan")
    public Message addGuanliyuan(HttpSession session,String name,String password){
        Message message = new Message();
        Guanliyuan guanliyuan = new Guanliyuan();
        guanliyuan.setName(name);
        guanliyuan.setPassword(password);
        int role=guanliyuanService.addGuanliyuan(guanliyuan);
        if(role==1){
            message.setFlag(true);
        }else {
            message.setFlag(false);
            message.setMsg("添加失败");
        }
        return message;
    }

    @ResponseBody
    @RequestMapping("/addStudent")
    public Message addStudent(HttpSession session, Student student){
        Message message = new Message();
        int role=guanliyuanService.selectCountBynumber(student.getNumber());
        int role2=guanliyuanService.slectCountByphone(student.getPhone());
        if(role!=0){
            message.setFlag(false);
            message.setMsg("该学号已存在");
        }else if(role2!=0){
            message.setFlag(false);
            message.setMsg("该手机号已存在");
        } else {
            student.setPassword(student.getNumber());
            guanliyuanService.addStudent(student);
            message.setFlag(true);
        }
        return message;
    }

    @ResponseBody
    @RequestMapping("/showStudent")
    public HashMap<String, Object> showStudent(int page,int limit) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Student> list = new ArrayList<>();
        int offset=(page-1)*limit;
        list = guanliyuanService.selectStudentByPage(offset,limit);
        Integer count = guanliyuanService.selectStudentAllcount();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/updataStudent")
    public Message updataStudent(Student student){
        Message message = new Message();
        int role = guanliyuanService.updataStudent(student);
        if(role==1){
            message.setFlag(true);
        }else {
            message.setFlag(false);
            message.setMsg("修改失败");
        }
        return message;
    }

    @ResponseBody
    @RequestMapping("/deleteStudent")
    public Message deleteStudent(String number){
        Message message = new Message();
        int role = guanliyuanService.deleteStudentBynumber(number);
        if(role == 1){
            message.setFlag(true);
        }else {
            message.setFlag(false);
        }
        return message;
    }

    @ResponseBody
    @RequestMapping("/selectStudent")
    public HashMap<String, Object> selectStudent(String searchResult ,int page ,int limit) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Student> list = new ArrayList<>();
        int offset=(page-1)*limit;
        list = guanliyuanService.advanceSelect(searchResult,offset,limit);
        System.out.println(page);
        System.out.println(limit);
        Integer count = guanliyuanService.advanceSelectCount(searchResult);
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }
}
